package com.lagou.server.handler;

import com.lagou.request.RpcRequest;
import com.lagou.server.util.SpringContextUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

public class UserServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RpcRequest rpcRequest = (RpcRequest) msg;
        Object bean = SpringContextUtil.getBean(rpcRequest.getRequestId());
        Method method = Class.forName(rpcRequest.getClassName()).getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
        method.invoke(bean, rpcRequest.getParameters());
        ctx.writeAndFlush("success");
    }
}